package org.protege.editor.owl.model.refactor.ontology;

import org.protege.editor.core.Disposable;
import org.protege.editor.owl.model.OWLModelManager;
import org.protege.editor.owl.model.entity.CustomOWLEntityFactory;
import org.protege.editor.owl.model.entity.OWLEntityCreationException;
import org.protege.editor.owl.model.entity.OWLEntityFactory;
import org.protege.editor.owl.ui.renderer.OWLEntityRendererImpl;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLEntity;
/*
* Copyright (C) 2007, University of Manchester
*
* Modifications to the initial code base are copyright of their
* respective authors, or their employers as appropriate.  Authorship
* of the modifications may be determined from the ChangeLog placed at
* the end of this file.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.

* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.

* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
 * Author: drummond<br>
 * http://www.cs.man.ac.uk/~drummond/<br><br>
 * <p/>
 * The University Of Manchester<br>
 * Bio Health Informatics Group<br>
 * Date: Aug 21, 2008<br><br>
 *
 * Takes an IRI and replaces the fragment/last path element with a generated ID
 * If no fragment exists, adds the ID directly
 *
 */
public class OWLEntityIRIRegenerator implements Disposable {

    private OWLEntityFactory fac;

    private OWLEntityRendererImpl fragmentRenderer;

    public OWLEntityIRIRegenerator(OWLModelManager mngr) {

        fragmentRenderer = new OWLEntityRendererImpl(); // basic fragment renderer
        fragmentRenderer.setup(mngr);
        fragmentRenderer.initialise();

        // regardless of how prefs are set up, we always want to generate an auto ID URI
        this.fac = new CustomOWLEntityFactory(mngr){
            protected boolean isFragmentAutoGenerated() {
                return true;
            }
        };
    }


    public IRI generateNewIRI(OWLEntity entity) {
        IRI base = getBaseIRI(entity);

        String id = ""; // this is the "user given name" which will not be used

        OWLEntity newEntity = getEntity(entity, id, base);

        return newEntity.getIRI();
    }


    private OWLEntity getEntity(OWLEntity entity, String id, IRI base) {
        try {
            return fac.createOWLEntity(entity.getClass(), id, base).getOWLEntity();
        }
        catch (OWLEntityCreationException e) {
            throw new RuntimeException(e);
        }
    }


    private IRI getBaseIRI(OWLEntity entity) {
        String fragment = fragmentRenderer.render(entity);
        IRI iri = entity.getIRI();

        if (fragment != null){
            return IRI.create(iri.toString().substring(0, iri.toString().lastIndexOf(fragment)));
        }
        return iri;
    }


    public void dispose() throws Exception {
        fragmentRenderer.dispose();
    }
}
